package com.lmk.pet.service.Impl;

import com.lmk.basic.service.IBaiduAiAuditService;
import com.lmk.basic.service.Impl.BaseServiceImpl;
import com.lmk.basic.util.AjaxResult;
import com.lmk.basic.util.LoginContext;
import com.lmk.org.domain.Employee;
import com.lmk.org.mapper.EmployeeMapper;
import com.lmk.pet.domain.Pet;
import com.lmk.pet.domain.PetDetail;
import com.lmk.pet.domain.PetOnlineAuditLog;
import com.lmk.pet.mapper.PetDetailMapper;
import com.lmk.pet.mapper.PetMapper;
import com.lmk.pet.mapper.PetOnlineAuditLogMapper;
import com.lmk.pet.service.IPetService;
import com.lmk.user.domain.Logininfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private IBaiduAiAuditService iBaiduAiAuditService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Override
    @Transactional
    public void add(Pet pet) {
        pet.setCreatetime(new Date());
        super.add(pet);
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPetId(pet.getId());
        petDetailMapper.save(petDetail);
    }

    @Override
    @Transactional
    public void del(Long id) {
        super.del(id);
        petDetailMapper.removeByPetId(id);
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);
        if(pet.getPetDetail()!=null){
            PetDetail petDetail = pet.getPetDetail();
            petDetail.setPetId(pet.getId());
            //宠物详情更新：根据宠物详情id去更新
            petDetailMapper.update(petDetail);
        }

    }

    @Override
    @Transactional
    public void batchdel(List<Long> ids) {
        super.batchdel(ids);
        // 删除宠物详情
        petDetailMapper.batchdelByPetId(ids);
    }

    @Override
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        //上架 -不能用批量操作，有上架审核
        for (Long id : ids) {
            //上架自动审核文本
            Pet pet = petMapper.loadById(id);
            // 获取name
            String auditText = pet.getName();
            Boolean textBoo = iBaiduAiAuditService.textAudit(auditText);
            //审核图片：多张图片resources
            String petResources = pet.getResources();
            Boolean imageBoo = true;
            if(!StringUtils.isEmpty(petResources)){
                imageBoo = iBaiduAiAuditService.imageAudit(petResources);
            }

            //获取审核人
            Logininfo logininfo = LoginContext.getLogininfo(request);
            Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

            //审核通过
            if (textBoo && imageBoo){
                //数据库操作
                Map<String,Object> params = new HashMap<>();
                params.put("id",id);
                // 上架时间
                params.put("onsaletime",new Date());
                petMapper.onsale(params);


                //记录审核日志
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setState(1);
                auditLog.setPetId(id);
                auditLog.setAuditId(employee.getId());
                auditLog.setNote("审核成功！");
                petOnlineAuditLogMapper.save(auditLog);
            }else{
                //审核失败-note-当前是下架状态，如果审核失败！
                //记录审核日志
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setState(0);
                auditLog.setPetId(id);
                auditLog.setAuditId(employee.getId());
                auditLog.setNote("审核失败,宠物名称或图片不合法!!!");
                petOnlineAuditLogMapper.save(auditLog);
                //审核失败，上架失败
                return AjaxResult.me().setSuccess(false).setMsg("上架失败！审核失败,宠物名称或图片不合法!!!");
            }
        }
        return AjaxResult.me();

    }

    @Override
    public AjaxResult offsale(List<Long> ids, HttpServletRequest request) {
        //下架：修改state为0，offsaletime为当前时间 ,可以用批量操作
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("ids",ids);
        hashMap.put("offsaletime",new Date());
        petMapper.offsale(hashMap);
        return AjaxResult.me();
    }
}
